import type { DirectiveBinding } from 'vue'

/* 防抖函数实现 */
const debounce = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    const time = Number(binding.arg) || 500 // 单位ms
    if (typeof time !== 'number') {
      return
    }
    let timer: any = null
    el.addEventListener('click', () => {
      if (timer) {
        clearTimeout(timer)
      }
      timer = setTimeout(() => {
        binding.value()
      }, time)
    })
  },
  beforeUnmount(el: HTMLElement) {
    el.removeEventListener('click', () => { })
  }
}
export default debounce
